from api.mod.modLoader import ModLoader
from framework.syntacticSugar.EventEntiys import EventEntiys


class EventGenerator:
    def __init__(self, event_name, event_entiys: EventEntiys = None):
        """
        事件发生器
        见 API.md
        """
        self.mode_loader = ModLoader()
        self.event_entiys: EventEntiys = event_entiys if event_entiys is not None else EventEntiys()
        self.event_name = event_name
        self.all_events_name = []

    def render_json(self):
        """
        渲染事件
        """

        for _dict in self.mode_loader.eventall:
            _event_list = _dict.get("events")
            assert _event_list is not None
            if self.event_name in _event_list:
                # 如果当前event_bus存在这个mod
                handler = _dict.get('handler')
                assert handler is not None
                json:  dict = handler(self.event_entiys)
                print(json)

                myscript = json.get(self.event_name).get("script")
                assert myscript is not None
                new_json = [i.to_json() for i in myscript]
                json[self.event_name]['script'] = new_json
                return json

        print("[errors] 事件不存在")